package com.cjx.print.ui;

import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.cjx.print.App;
import com.cjx.print.R;
import com.cjx.print.utils.LogUtil;
import com.cjx.print.utils.NavigationBarUtil;
import com.cjx.print.utils.ToastUtil;

/**
 * Created by chenjinxu on 2019/5/26.
 * 说明:
 */
public abstract class BaseDialogFragment extends DialogFragment {
    public boolean saveStatusLog = true;
    boolean isFirst = true;
    boolean canceledOnTouchOutside = true;
    public String TAG = "TAG";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mContentView = inflater.inflate(getViewId(), container, false);
        if (saveStatusLog) {
            LogUtil.getInstance().log(TAG, "--onCreateView " + this);
        }
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().setCanceledOnTouchOutside(canceledOnTouchOutside);
            // 解决dialog打开dialog闪屏的问题
            getDialog().getWindow().setWindowAnimations(R.style.NullAnimationDialog);
            // 去除边距
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        App.app.currentDialog = this;
        return mContentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirst = true;
        if (saveStatusLog) {
            LogUtil.getInstance().log(TAG, "--onDestroyView " + this);
        }
        App.app.currentDialog = null;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window == null) {
                return;
            }
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            WindowManager manager = window.getWindowManager();
            DisplayMetrics metrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(metrics);

            WindowManager.LayoutParams attributes = window.getAttributes();
            int width = (int) (metrics.widthPixels * getDialogWidth());
            attributes.width = width == 0 ? WindowManager.LayoutParams.WRAP_CONTENT : width;
            window.setAttributes(attributes);
        }
    }

    public BaseDialogFragment setCanceledOnTouchOutside(boolean cancel) {
        canceledOnTouchOutside = cancel;
        return this;
    }

    public void showToast(String msg) {
        if (msg == null || getContext() == null) {
            return;
        }
        ToastUtil.getInstance().showText(msg);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T findViewById(int id) {
        T result = getView().findViewById(id);
        if (result == null) {
            throw new IllegalArgumentException("view 0x" + Integer.toHexString(id) + "doesn't exist");
        }

        return result;
    }

    public abstract int getViewId();

    protected float getDialogWidth() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? 0.5f : 0.8f;
    }

}
